package com.glsc.ngateway.platform.controller.glres;


import com.glsc.ngateway.common.api.platform.resp.PlatformResponse;
import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.glres.GlscResourceOperateLog;
import com.glsc.ngateway.platform.aspect.UserCache;
import com.glsc.ngateway.platform.dto.GlscResLogDto;
import com.glsc.ngateway.platform.service.glres.GlscResourceOperateLogService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 资源管理-变更日志
 */
@RestController
@RequestMapping("/api/res/log")
@Slf4j
public class GlscResourceOperateLogController {

    @Resource
    private UserCache userCache;

    @Resource
    private GlscResourceOperateLogService logService;

    /**
     * 分页查找
     *
     * @param dto
     * @param request
     * @return
     */
    @PostMapping("/page")
    public PlatformResponse<Page<GlscResourceOperateLog>> page(@RequestBody GlscResLogDto dto, HttpServletRequest request) {

        Map<String, Object> param = new HashMap<>();
        if (StringUtils.isNotBlank(dto.getDataType())) {
            param.put("EQ_dataType", dto.getDataType());
        }
        if (StringUtils.isNotBlank(dto.getType())) {
            param.put("EQ_type", dto.getType());
        }

        if (Objects.nonNull(dto.getOriginalId())) {
            param.put("EQ_originalId", dto.getOriginalId());
        }
        param.put("EQ_systemId", dto.getSystemId());

        Page<GlscResourceOperateLog> page = logService.page(dto.getPageNo(), dto.getPageSize(), param);
        return PlatformResponse.successData(page);
    }

    @GetMapping("/getContentById")
    @ApiOperation(value = "根据fileId下载文件", notes = "根据fileId下载文件")
    public byte[] download(@ApiParam(required = true, value = "fileId") @RequestParam Integer fileId, HttpServletResponse response) throws IOException {

        GlscResourceOperateLog file = logService.findById(fileId);
        Assert.isTrue(Objects.nonNull(file), "文件不存在");

        HttpHeaders headers = new HttpHeaders();
        ResponseEntity<byte[]> entity = null;
        InputStream in = null;
        in = new FileInputStream(new File(file.getFilePath()));
        byte[] bytes = new byte[in.available()];
        String imageName = file.getFileName();
        //处理IE下载文件的中文名称乱码的问题
        ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = ((ServletRequestAttributes) attrs).getRequest();
        String header = request.getHeader("User-Agent").toUpperCase();
        if (header.contains("MSIE") || header.contains("TRIDENT") || header.contains("EDGE")) {
            imageName = URLEncoder.encode(imageName, "utf-8");
            imageName = imageName.replace("+", "%20"); //IE下载文件名空格变+号问题
        } else {
            //imageName = new String(imageName.getBytes(), "UTF-8");
            imageName = URLEncoder.encode(imageName, "utf-8");
        }
        in.read(bytes);
        //headers.add("Content-Disposition", "attachment;filename=" + imageName);

        response.setContentType("APPLICATION/OCTET-STREAM");
        response.setHeader("Content-Disposition", "attachment;filename=" + imageName);
        return bytes;
    }


}
